package com.bocai.duobao.mapper;

import com.bocai.duobao.model.BetNumInfo;
import com.bocai.duobao.model.entity.DbBetDetail;
import com.bocai.duobao.model.entity.DbGame;
import com.bocai.duobao.model.entity.DbOpenRecord;
import com.bocai.duobao.model.param.OpenRecord;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author create by VIC.Huang
 * @date 2018/1/16
 */
@Component
@Mapper
public interface DuobaoMapper {
    @Select("select id, title, list_img_path listImgPath, detail_img_path detailImgPath," +
            " two_price twoPrice, four_price fourPrice, ten_price tenPrice, win_price winPrice" +
            " from t_db_game where status = 1")
    List<DbGame> selectGames();

    @Select("select id, title, list_img_path listImgPath, detail_img_path detailImgPath," +
            " two_price twoPrice, four_price fourPrice, ten_price tenPrice, win_price winPrice" +
            " from t_db_game where id = #{gameId}")
    DbGame selectGameById(Long gameId);

    @Select("select " +
            " case when p.type = 'two_price' then g.two_price" +
            " when p.type = 'four_price' then g.four_price" +
            " when p.type = 'ten_price' then g.ten_price" +
            " end perPrice," +
            " g.win_price perWinPrice, " +
            " p.limit_amount limitAmount, " +
            " g.list_img_path dbGameListImgPath ," +
            " g.title dbGameTitle " +
            " from " +
            " t_db_playitem p " +
            " join " +
            " t_db_game g on p.game_id = g.id and g.`status` = 1 " +
            " where p.game_id = #{gameId} and p.bet_num = #{betNum}")
    BetNumInfo selectBetNumInfo(@Param("gameId") Long gameId, @Param("betNum") String betNum);

    @Insert("insert into t_db_bet_detail(db_game_id,user_id,bet_num,amount,per_price,per_win_price,issue_no,db_game_list_img_path,order_no,db_game_title) " +
            "values(#{dbGameId},#{userId},#{betNum},#{amount},#{perPrice},#{perWinPrice},#{issueNo},#{dbGameListImgPath},#{orderNo},#{dbGameTitle})")
    int insertBetDetail(DbBetDetail betDetail);

    @Select("select count(1) from t_db_open_record")
    int countOpenRecords();

    @Select("select issue_no issueNo, open_code openCode, open_result_desc openResultDesc from t_db_open_record order by id desc limit #{start},#{count}")
    List<OpenRecord> selectOpenRecords(@Param("start")int start, @Param("count")int count);

    @Select("select count(1) from t_db_open_record where issue_no = #{issueNo}")
    int countDbOpenRecordByIssueNo(String issueNo);

    @Insert("insert into t_db_open_record(issue_no,open_code,open_result,open_result_desc,open_time) " +
            "values(#{issueNo},#{openCode},#{openResult},#{openResultDesc},#{openTime})")
    int insertDbOpenRecord(DbOpenRecord openRecord);

    @Select("select id, issue_no issueNo, open_code openCode, open_result openResult, open_result_desc openResultDesc, open_time openTime" +
            " from t_db_open_record where finish_flag = 'NO'")
    List<DbOpenRecord> selectUnhandlePrizeRecord();
}
